package com.ruoyi.carApi.click.impl;

import com.ruoyi.carApi.api.IPlateCallback;
import com.ruoyi.carApi.callBack.PlateCallback;
import com.ruoyi.carApi.click.CarApiClick;
import com.ruoyi.carApi.click.ICarCallBack;
import com.ruoyi.carApi.init.ApiInit;
import com.sun.jna.Pointer;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: wwy
 * @Date: 2021/6/22 11:06
 */
public class CarApiDefaultClick implements CarApiClick {
    private static PlateCallback plateCallback = new PlateCallback();
    /**
     * 相机连接全局对象
     */
    public static ConcurrentHashMap<String,Pointer> connectMap = new ConcurrentHashMap<>();

    /**
     * 打开道闸
     * @param hSDK
     * @return
     */
    @Override
    public int openGate(Pointer hSDK) {
        int i = ApiInit.ICESDKInstance.ICE_IPCSDK_OpenGate(hSDK);
        if(i!=1){
            //失败
            System.out.println("打开道闸失败");
        }else{
            //成功
            System.out.println("打开道闸成功");
        }
        return i;
    }

    /**
     * 打开道闸
     * @param ip
     * @return
     */
    @Override
    public int openGate(String ip) {
        Pointer hSDK = connectMap.get(ip);
        int i = ApiInit.ICESDKInstance.ICE_IPCSDK_OpenGate(hSDK);
        if(i!=1){
            //失败
            System.out.println("打开道闸失败");
        }else{
            //成功
            System.out.println("打开道闸成功");
        }
        return i;
    }

    /**
     * 连接相机
     * @param ip
     * @return
     */
    @Override
    public Pointer connect(String ip) {
        //首先判断是否连接过，连接过，需要退出后，再次进行连接
        Pointer hsDk = connectMap.get(ip);
        if(hsDk != null){
            //进行退出
            this.closeConnect(hsDk);
        }
        Pointer i = ApiInit.ICESDKInstance.ICE_IPCSDK_OpenDevice(ip.trim());
        if(i == null){
            System.out.println(ip + ": connect failed");
            return null;
        }else{
            System.out.println(ip + ": connect successed");
            //连接成功，放入全局登录对象中

        }
        connectMap.put(ip,i);
        return i;
    }

    /**
     * 关闭连接
     * @param hsDk
     * @return
     */
    @Override
    public void closeConnect(Pointer hsDk) {
        ApiInit.ICESDKInstance.ICE_IPCSDK_Close(hsDk);
        connectMap.remove(hsDk);
    }

    /**
     * 关闭连接
     * @param ip
     */
    @Override
    public void closeConnect(String ip) {
        Pointer pointer = connectMap.get(ip);
        if(pointer != null){
            ApiInit.ICESDKInstance.ICE_IPCSDK_Close(pointer);
        }

    }

    /**
     * 设置指定设备回调函数
     * @param plateCallback
     * @param hsDk
     */
    @Override
    public void setCallback(IPlateCallback plateCallback,Pointer hsDk) {
        ApiInit.ICESDKInstance.ICE_IPCSDK_SetPlateCallback(hsDk,plateCallback,1);
    }

    /**
     * 设置全部设备回调函数
     * @param plateCallback
     */
    @Override
    public void setCallbackAll(IPlateCallback plateCallback) {
        for (Map.Entry<String, Pointer> stringIntegerEntry : connectMap.entrySet()) {
            Pointer hsDk = stringIntegerEntry.getValue();
            ApiInit.ICESDKInstance.ICE_IPCSDK_SetPlateCallback(hsDk,plateCallback,1);
            ApiInit.ICESDKInstance.ICE_IPCSDK_EnableUTF8(hsDk,1);
        }
    }

    /**
     * 快速连接
     * @param ips
     */
    @Override
    public void quickConnect(String[] ips) {
        if(ips != null){
            for (String ip : ips) {
                //连接相机
                Pointer hsDk = this.connect(ip);
            }

        }
        //创建全部回调函数
        this.setCallbackAll(plateCallback);
    }


    /**
     * 结果获取回调函数
     * @param iCarCallBack
     */
    @Override
    public void setResultCallBack(ICarCallBack iCarCallBack) {
        PlateCallback.iCarCallBack = iCarCallBack;
    }

    /**
     * 添加白名单
     * @param ip 设备ip
     * @param pcNumber 车牌
     * @param pcDateBegin 开始日期
     * @param pcDateEnd 结束日期
     * @param pcTimeBegin 开始时间
     * @param pcTimeEnd 结束时间
     * @param pcRemark 备注
     * @param pcRsrv2 备用
     * @param pcRsrv3 备用
     * @param pcRsrv4 备用
     * @return
     */
    @Override
    public int addWhiteItem(String ip, String pcNumber, String pcDateBegin, String pcDateEnd, String pcTimeBegin, String pcTimeEnd, String pcRemark, String pcRsrv2, String pcRsrv3, String pcRsrv4) {
        Pointer pointer = connectMap.get(ip);
        if(pointer == null){
            return 0;
        }
        int i = ApiInit.ICESDKInstance.ICE_IPCSDK_WhiteListInsertItem_ByNumber(pointer, pcNumber,"2021/01/01", "2022/02/06", "00:00:00", "23:59:59", "W", "备注123",  "",  "",  "");
        return i;
    }

    @Override
    public int deleteWhiteItem(String ip, String pcNumber) {
        Pointer pointer = connectMap.get(ip);
        if(pointer == null){
            return 0;
        }
        int i = ApiInit.ICESDKInstance.ICE_IPCSDK_WhiteListDeleteItem_ByNumber(pointer, pcNumber);
        return i;
    }
}
